﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

namespace EnumHelpers
{
    /// <summary>
    /// 枚举扩展方法类，提供各种枚举相关的实用功能
    /// </summary>
    public static class EnumHelper
    {
        #region 获取描述信息

        /// <summary>
        /// 获取枚举值的 Description 特性值
        /// </summary>
        public static string GetDescription(this Enum value)
        {
            if (value == null)
                return string.Empty;

            FieldInfo fieldInfo = value.GetType().GetField(value.ToString());
            if (fieldInfo == null)
                return value.ToString();

            DescriptionAttribute attribute = fieldInfo.GetCustomAttribute<DescriptionAttribute>();
            return attribute?.Description ?? value.ToString();
        }
        #endregion
    }
}
